﻿using System;
using System.Collections.Generic;
using System.Text;
using JEng_Mark_1.Interfaces;
using Microsoft.Xna.Framework.Graphics;
using JEng_Mark_1.Managers;

namespace JEng_Mark_1.Models
{
    public class JModel : IJEngModel
    {
        private string _fileName;
        /// <summary>
        /// The file name of the asset.
        /// </summary>
        public string FileName
        {
            get { return _fileName; }
            set { _fileName = value; }
        }

        private Model _baseModel;
        ///<summary>
        ///Gets the underlying Effect.
        ///</summary>
        public Model BaseModel
        {
            get { return _baseModel; }
        }

        private bool _readyToRender = false;
        ///<summary>
        ///Is the texture ready to be rendered.
        ///</summary>
        public bool ReadyToRender
        {
            get { return _readyToRender; }
        }

        /// <summary>
        /// Construct a new JModel.
        /// </summary>
        public JModel()
        {

        }

        /// <summary>
        /// Construct a new JModel.
        /// </summary>
        /// <param name="fileName">The asset file name.</param>
        public JModel(string fileName)
        {
            _fileName = fileName;
        }

        public void LoadContent()
        {
            if (!String.IsNullOrEmpty(_fileName))
            {
                _baseModel = JEngineManager.ContentManager.Load<Model>(_fileName);
                _readyToRender = true;
            }
        }

        /// <summary>
        /// Set the effect for this model to use, overwrites the effect stored in file
        /// </summary>
        /// <param name="useEffect">Effect to use</param>
        public void SetEffect(Effect useEffect)
        {
            if (ReadyToRender)
            {
                for (int i = 0; i < this.BaseModel.Meshes.Count; i++)
                {
                    for (int j = 0; j < this.BaseModel.Meshes[i].MeshParts.Count; j++)
                    {
                        this.BaseModel.Meshes[i].MeshParts[j].Effect = useEffect;
                    }
                }
            }
        }
    }
}
